//
//  XTNetWorkTool.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/12.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit
import AFNetworking

//定义一个枚举
enum RequestMthod : String {
    case GET = "GET"
    case POST = "POST"
}


class XTNetWorkTool: NSObject {
    
    //创建单例
    static var sharedInstance : XTNetWorkTool = XTNetWorkTool()
    
    //创建AFHTTPManager
    private var AFNmanager : AFHTTPSessionManager = {
        //创建manager,设置baseURL
        let manager = AFHTTPSessionManager(baseURL: NSURL(string: baseURL))
        //failed: unacceptable content-type: text/plain 解析器没有这个 text/plain
        //所以我们要自己添加
        //给相应的序列化器多添加支持的类型
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return manager
    }()
    
    
    func request (method : RequestMthod, URLString: String, parameters :AnyObject,progress : ((NSProgress) -> Void)?, success : ((NSURLSessionDataTask, AnyObject?) -> Void)?,failure : ((NSURLSessionDataTask?, NSError) -> Void)?) {
        
        if method == RequestMthod.GET {
            AFNmanager.GET(URLString, parameters: parameters, progress: progress, success: success, failure: failure)
        }else {
            AFNmanager.POST(URLString, parameters: parameters, progress: progress, success: success, failure: failure)
        }
        
    }
    

}
